﻿using AutoMapper;
using MediatR;
using NPOI.XSSF.UserModel.Extensions;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Procurement
{
    /// <summary>
    /// 订单行
    /// </summary>
    public class OrderLinesAddHandler : IRequestHandler<OrderLinesAddCommend, Result>
    {
        private readonly IBaseRepository<OrderLines> OrderLinesRepository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="Resource"></param>
        public OrderLinesAddHandler(IMapper mapper, IBaseRepository<OrderLines> OrderLinesRepository)
        {
            this.mapper = mapper;
            this.OrderLinesRepository = OrderLinesRepository;
        }

        public async Task<Result> Handle(OrderLinesAddCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Added };

            var AddData = mapper.Map<OrderLines>(request);
            string Sql = "insert into OrderLines values(@OrderLineId,@InventoryInfoId,@OrderLinePrice,@OrderLineNum,@Unit,@BankAmount,@LineSummary,@PurchasinId,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = OrderLinesRepository.Add(Sql, new
            {
                OrderLineId = AddData.OrderLineId,
                InventoryInfoId = request.InventoryInfoId,
                OrderLinePrice = AddData.OrderLinePrice,
                OrderLineNum = AddData.OrderLineNum,
                Unit = AddData.Unit,
                BankAmount = AddData.BankAmount,
                LineSummary = AddData.LineSummary,
                PurchasinId = AddData.PurchasinId,
                Handle = request.Handle,
                HandleContext = request.HandleContext,
                HandleTime = DateTime.Now,
                Pid = request.Pid,
                IsDelete = AddData.IsDelete
            });
            if (AddNum > 0)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "添加成功";
                return results;
            }
            else
            {
                return results;
            }
        }
    }
}
